package com.source.zxingproject;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions;
import com.source.zxingproject.zxing.CaptureActivity;

import me.jessyan.autosize.internal.CancelAdapt;

public class MainActivity extends AppCompatActivity implements CancelAdapt {
    public String[] per = new String[]{
            Manifest.permission.CAMERA
    };
    private Button btnZXing, btnHMS, btnAuto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        checkPermission();
    }

    private void checkPermission() {
        boolean isAllGranted = checkPermissionAllGranted(per);
        if (!isAllGranted) {
            ActivityCompat.requestPermissions(this, per,
                    0);
        } else {
//            startActivity(new Intent(this, CaptureActivity.class));
//            startScan();
//            HMSCaptureActivity.startActivity(MainActivity.this);
            initListener();
        }
    }

    protected void initView() {
        btnZXing = findViewById(R.id.btnZXing);
        btnHMS = findViewById(R.id.btnHMS);
        btnAuto = findViewById(R.id.btnAuto);
    }

    private void initListener() {
        btnZXing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CaptureActivity.class));
            }
        });
        btnHMS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HMSCaptureActivity.startActivity(MainActivity.this);
            }
        });
        btnAuto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AutoSizeActivity.startActivity(MainActivity.this);
            }
        });
    }

    /**
     * 检查是否有指定的权限
     *
     * @param permissions 权限列表
     * @return 是否有权限
     */
    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (getApplicationContext().checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0) {
            boolean isAllGranted = true;
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    isAllGranted = false;
                    break;
                }
            }

            if (isAllGranted) {
                Toast.makeText(this, "已授权 ", Toast.LENGTH_SHORT).show();
//                startActivity(new Intent(this, CaptureActivity.class));
//                startScan();
//                HMSCaptureActivity.startActivity(MainActivity.this);
                initListener();
            } else {
                Toast.makeText(this, "未授权 ", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }


    /**
     * 扫描二维码
     */
    private final int REQUEST_CODE_SCAN_DEFAULT_MODE = 200;

    private void startScan() {
        HmsScanAnalyzerOptions options = new HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.ALL_SCAN_TYPE).create();
        ScanUtil.startScan(this, REQUEST_CODE_SCAN_DEFAULT_MODE, options);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        if (requestCode == REQUEST_CODE_SCAN_DEFAULT_MODE) {
//获取扫码结果
            HmsScan hmsScan = data.getParcelableExtra(ScanUtil.RESULT);
            if (!TextUtils.isEmpty(hmsScan.originalValue)) {
                Toast.makeText(this, hmsScan.originalValue, Toast.LENGTH_SHORT).show();
            }
        }
    }

}
